package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.MenuService;
import com.itheima.service.UserService;
import org.springframework.web.bind.annotation.RequestBody;
import com.itheima.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 菜单类
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private UserService userService;
    @Reference
    private MenuService menuService;

    //根据用户名获取菜单
    @RequestMapping("/getMenu")


    public Result getMenu(String username) {
        //创建一个集合用于封装菜单信息
        List<Map> menuList = new ArrayList<>(); //所有菜单集合
        //调用userService查询出用户关联的菜单信息
        User user = userService.findByUsername(username);
        Set<Role> roles = user.getRoles();

        //判断其是否具有角色能够访问菜单
        if (roles != null) {
            for (Role role : roles) {
                //一级菜单集合
                LinkedHashSet<Menu> menus = role.getMenus();

                if (menus != null) {
                    //遍历一级菜单,取出所有菜单
                    for (Menu menu : menus) {
                        //从menu对象中取出前台所需要的属性,封装到map集合中: path title icon children
                        Map map = new HashMap();    //一级菜单集合
                        map.put("path", menu.getPath());
                        map.put("title", menu.getName());
                        map.put("icon", menu.getIcon());

                        //从menu的children属性中,取出所有二级菜单
                        List<Menu> children = menu.getChildren();
                        //将子菜单封装到list集合中
                        List<Map> childList = new ArrayList<>();    //二级菜单集合

                        if (children != null) {
                            for (Menu child : children) {
                                //将子菜单封装到子菜单集合中: path title linkUrl children
                                Map childMap = new HashMap();
                                childMap.put("path", child.getPath());
                                childMap.put("title", child.getName());
                                childMap.put("linkUrl", child.getLinkUrl());
                                childMap.put("children", child.getChildren());
                                childList.add(childMap);
                            }
                        }

                        //将二级菜单集合添加到一级菜单集合的children属性中
                        map.put("children", childList);

                        //将每一个封装了一级菜单的map集合添加到menuList中
                        menuList.add(map);

                    }
                }

            }

            return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,menuList);

        }


        return new Result(false,MessageConstant.GET_MENU_FAIL);
    }


    //查询所有菜单信息
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Menu> roleList = menuService.findAll();
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,roleList);    //查询菜单成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);   //查询菜单失败
        }
    }

    //分页查询菜单信息
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return menuService.findPage(queryPageBean);
    }

    //添加菜单
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return new Result(true,MessageConstant.ADD_MENU_SUCCESS);   //新增菜单成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_FAIL);     //新增菜单失败
        }
    }

    //根据id查询菜单
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menu);     //查询菜单成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);       //查询菜单失败
        }
    }

    //编辑菜单
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        try {
            menuService.edit(menu);
            return new Result(true,MessageConstant.EDIT_MENU_SUCCESS);  //编辑菜单成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MENU_FAIL);    //编辑菜单失败
        }
    }

    //删除菜单
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            menuService.delete(id);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);    //删除菜单成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);      //删除菜单失败
        }
    }


}
